/***
 *  前后端分离
 *      client
 *      server
 * */

var http = require('http');
var url = require('url')
var querystring = require('querystring')

http.createServer(function (request,response) {


    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader('Access-Control-Allow-Headers', 'Content-type');
    response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS,PATCH");

    var urlObj = url.parse(request.url);

    if(urlObj.pathname=='/login'){
        var postData='';

        request.on('data',function (chunck) {
            postData+=chunck;
        })

        request.on('end',function (error) {
            if(!error){
                response.writeHeader(200,{'Content':'text/html;charset=utf8'});
                postData = querystring.parse(postData);
                console.log("post-data:");
                console.log(postData);
                response.end(JSON.stringify(postData));
            }
        })
    }

}).listen(3000,function () {
    console.log("the server is running on port 3000");
})